#include<stdio.h>
#include<string.h>
#include"../include/main.h"
#include"../include/narrtree.h"
#include"../include/constants.h"
#include"../include/mount_vfs.h"
#include"../include/vfs_errorcodes.h"
#include"../include/hash.h"
#include"../include/create_vfs.h"

void export_file(char vfs_filepath[],char hd_filepath[])
{
	FILE *fp;
	llist *ll;
	ntree *tofind;
	size_t s;
	int i=0,bno;
	char *k;
	//fp=fopen(hd_filepath,"r");
	k=strrchr(vfs_filepath,'/');
	if(k != NULL)
	{
		k++;
		if(*k=='\0')
		{
			k--;
			*k='\0';
		}
	}
	if(mounted==0)
		printf("%s_FAILURE %s\n",glb_command,ERR_VFS_EXPORTFILE_04);
	else
	{
		schflag=0;
		tofind=search_node(head,vfs_filepath);
		schflag=0;
		if(tofind==NULL)
			printf("%s_FAILURE %s\n",glb_command,ERR_VFS_EXPORTFILE_01);
		else
		{
			if(tofind->file_desc->filetype==1)
				printf("%s_FAILURE %s\n",glb_command,ERR_VFS_EXPORTFILE_03);
			else
			{
				ll=find(tofind->file_desc);
				if(ll != NULL)
				{
					fp=fopen(hd_filepath,"w");
					bno=ll->desc->blk_num;
					if(fp==NULL)
						printf("%s_FAILURE %s\n",glb_command,ERR_VFS_EXPORTFILE_02);
					else
					{
						fwrite(data[bno],1,BLOCK_SIZE,fp);
						printf("exportfile_SUCCESS\n");
					}
				}
			}
		}
	}
	if(fp != NULL)
	close(fp);
}
